<template>
    <el-dialog title="修改抽样数量" :close-on-click-modal="false" :visible.sync="visible" :append-to-body="true"
        class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
        <el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :rules="rules">
            <el-form-item label="抽样数量" prop="samplingQty">
                <el-input v-model.number='dataForm.samplingQty' placeholder='请输入抽样数量' clearable required
                    :style='{ "width": "100%" }' maxlength='200'>
                </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">{{ $t('common.cancelButton') }}</el-button>
            <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
        </span>
    </el-dialog>
</template>
    
<script>
import { updateSamplingQty } from '@/api/q1/business/ipqcInspectionLot'
export default {
    data() {
        return {
            btnLoading: false,
            visible: false,
            dataForm: {
                code: "",
                sampleId: "",
                samplingQty: 0,
                productionQty: 0,
            },
            rules: {
                samplingQty: [
                    { required: true, message: '请输入抽样数量', trigger: '"blur"' },
                    { pattern: /^([0-9]+)$/, message: '请输入数字', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        init(code, sampleId, samplingQty, productionQty) {
            this.dataForm.code = code
            this.dataForm.sampleId = sampleId
            this.dataForm.samplingQty = samplingQty
            this.dataForm.productionQty = productionQty
            this.visible = true
        },
        dataFormSubmit() {
            this.$refs['elForm'].validate((valid) => {
                if (valid) {
                    if (this.dataForm.samplingQty > this.dataForm.productionQty) {
                        this.$message({
                            type: 'error',
                            message: '抽样数量不能大于批次数量',
                            duration: 1500,
                        })
                        return
                    }
                    updateSamplingQty(this.dataForm).then((res) => {
                        this.btnLoading = false;
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false,
                                    this.$emit('refresh', true)
                            }
                        })
                    }).catch((res) => {
                        this.btnLoading = false;
                    })
                }
            })
        }
    }
}
</script>